﻿//returns header for contacts tab
function calculate_header(number_of_entities)
{
    if (number_of_entities.toString().charAt(number_of_entities.toString().length - 1) == 1)
        return 'There is 1 entity';
    else
        return 'There are ' + number_of_entities + ' entities';
}

//removes all children inside an element
function remove_all_children(parent_element)
{
    if (parent_element.hasChildNodes())
    {
        while (parent_element.childNodes.length >= 1)
        {
            parent_element.removeChild(parent_element.firstChild);
        }
    }
}

//takes hash string, checks if it is already saved
//if not - saves it and returns false
//if yes - returns true
function compare_and_save_hash(value)
{
    var hash_string = $(data_hash).value;

    var hash_array = hash_string.split(',');
    var value_exists = false;
    hash_array.each(function (keyOfElement, index)
    {
        if (value == hash_array[index])
            value_exists = true;
    });

    if (!value_exists)
    {
        hash_array[hash_array.length + 1] = value;
        $(data_hash).value = hash_array.toString();
    }

    return value_exists;
}

// Wait icons for the tabs
//displays wait image inside the tab
function show_wait(tab)
{
    var img_name = tab + '_wait';
    if (!$(img_name))
        generate_wait_icon(tab);
    $(img_name).show();
}

//hides wait image
function hide_wait(tab)
{
    var img_name = tab + '_wait';
    $(img_name).hide();
}

//generates wait icon inside a tab if it doesn't exist
function generate_wait_icon(tab)
{
    var block = new Element("div");
    block.setAttribute('id', tab + '_wait')
    block.addClassName('waiting_bar');
    var img = new Element("img", { src: waitImage_url });
    block.appendChild(img);
    $(tab).appendChild(block);
}

//takes date-time string and returns only date part
function separate_date_from_datetime(datetime)
{
    return datetime.split(' ')[0]
}

//takes text and url address and returns formated A element
function cover_in_hreaf(text, h)
{
    var a = new Element("a", { href: h }); 
    a.update(text);
    return a;
}

//takes ids of user and activity and creates sceleton A element with id
function add_person_container(user_id, activity_id)
{
    var a = new Element("a", { id: 'activity_userid_' + user_id + activity_id });
    return a;
}

//populates A element with data and sets URL address to it
function update_person_container(user_id, activity_id, text, h)
{
    var a = $('activity_userid_' + user_id + activity_id);
    a.setAttribute('href', h)
    a.update(text);
}

// checks the parameter show_all_errors and if it is true displays alert with error description
function show_errors_if_any(data)
{
    if (data.error & show_all_errors == true)
        alert('There was an expection while retrieving the data from the server:' + data.error);
}